﻿#include "TrollKing.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../MapObject.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    TrollKing::TrollKing(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool TrollKing::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void TrollKing::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        if (Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 3))
        {
            if (getEnvir()->Random->Next(2) == 0 || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 2))
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                tempVar->Type = 0;
                Broadcast(tempVar);

                int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                std::vector<MapObject*> targets = FindAllTargets(3, getCurrentLocation(), false);

                if (targets.empty())
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                for (int i = 0; i < targets.size(); i++)
                {
                    DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {targets[i], damage, DefenceType::MACAgility});
                    ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                WalkAway();
            }
        }
        else
        {
            S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->TargetID = getTarget()->ObjectID;
            tempVar2->Type = 1;
            Broadcast(tempVar2);

            std::vector<MapObject*> targets = FindAllTargets(3, getTarget()->getCurrentLocation(), false);

            if (targets.empty())
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                return;
            }

            for (int i = 0; i < targets.size(); i++)
            {
                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                if (damage == 0)
                {
                    continue;
                }

                int delay = Functions::MaxDistance(getCurrentLocation(), targets[i]->getCurrentLocation()) * 50 + 500; //50 MS per Step

                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {targets[i], damage, DefenceType::ACAgility});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

        ActionTime = getEnvir()->getTime() + 500;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
    }

    void TrollKing::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        target->Attacked(this, damage, defence);
    }

    void TrollKing::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        target->Attacked(this, damage, defence);

        if (target->Attacked(this, damage, DefenceType::MACAgility) <= 0)
        {
            return;
        }

        PoisonTarget(target, 1, getEnvir()->Random->Next(Stats[Stat::MaxMC]), PoisonType::Dazed, 1000);
    }

    void TrollKing::ProcessTarget()
    {
        if (getTarget() == nullptr || !getCanAttack())
        {
            return;
        }

        if (InAttackRange() && getEnvir()->getTime() < FearTime)
        {
            Attack();
            return;
        }

        FearTime = getEnvir()->getTime() + 5000;

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        int dist = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation());

        if (dist >= AttackRange)
        {
            MoveTo(getTarget()->getCurrentLocation());
        }
        else
        {
            WalkAway();
        }
    }

    void TrollKing::WalkAway()
    {
        if (getTarget() == nullptr || !getCanAttack())
        {
            return;
        }

        MirDirection dir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());

        if (Walk(dir))
        {
            return;
        }

        switch (getEnvir()->Random->Next(2)) //No favour
        {
            case 0:
                for (int i = 0; i < 7; i++)
                {
                    dir = Functions::NextDir(dir);

                    if (Walk(dir))
                    {
                        return;
                    }
                }
                break;
            default:
                for (int i = 0; i < 7; i++)
                {
                    dir = Functions::PreviousDir(dir);

                    if (Walk(dir))
                    {
                        return;
                    }
                }
                break;
        }
    }
}
